package club.pinea.client;

import club.pinea.proto.BasicProto;
import club.pinea.proto.CommandProto;
import club.pinea.proto.UserProto;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class ProtoBufClientHandler extends SimpleChannelInboundHandler<BasicProto.BaseMessage> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 登陆操作
        UserProto.LoginRequest loginRequest = UserProto.LoginRequest.newBuilder()
                .setName("admin")
                .setPass("123456")
                .build();

        BasicProto.BaseMessage baseMessage = BasicProto.BaseMessage.newBuilder()
                .setOrder(CommandProto.Command.C_S_LOGIN_VALUE)
                .setBody(loginRequest.toByteString())
                .build();


        // 修改密码
        /*UserProto.updatePwdRequest updatePwdRequest = UserProto.updatePwdRequest.newBuilder()
                .setName("admin")
                .setOldPass("123456")
                .setNewPass("666666")
                .build();

        BasicProto.BaseMessage baseMessage = BasicProto.BaseMessage.newBuilder()
                .setOrder(CommandProto.Command.C_S_USER_RESET_PASSWORD_VALUE)
                .setBody(updatePwdRequest.toByteString())
                .build();*/

        // 注册账号
//        UserProto.registeredRequest registeredRequest = UserProto.registeredRequest.newBuilder()
//                .setName("hq323")
//                .setPass("123456")
//                .setBeInvited("l6vbyQ")
//                .build();
//
//        BasicProto.BaseMessage baseMessage = BasicProto.BaseMessage.newBuilder()
//                .setOrder(CommandProto.Command.C_S_REGISTER_VALUE)
//                .setBody(registeredRequest.toByteString())
//                .build();
        ctx.writeAndFlush(baseMessage);
        System.out.println("结束写数据：" + System.currentTimeMillis());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, BasicProto.BaseMessage baseMessage) throws Exception {
//        System.out.println("我收到了服务器端发送的数据" + System.currentTimeMillis());
//        UserProto.LoginResponse loginResponse = UserProto.LoginResponse.parseFrom(baseMessage.getBody());
//        UserProto.updatePwdRequest updatePwdRequest = UserProto.updatePwdRequest.parseFrom(baseMessage.getBody());
        UserProto.RegisterRequest registeredRequest = UserProto.RegisterRequest.parseFrom(baseMessage.getBody());

//        System.out.println("NOW is: " + loginResponse);
//        System.out.println("NOW is: " + baseMessage);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        cause.printStackTrace();
        System.out.println("出错了");
        ctx.close();
    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            if (((IdleStateEvent) evt).state() == IdleState.WRITER_IDLE) {
                JSONObject obj = new JSONObject();
                obj.put("order", "h");
                ctx.writeAndFlush(obj.toString());
                System.out.println("club.pinea.client-heart");
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

}
